package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class GetSelectedTC
 */
//@WebServlet(description = "取得測試項目所選擇的測試案例", urlPatterns = { "/GetSelectedTC" })
public class GetSelectedTC extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetSelectedTC() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String strSQL = "SELECT t2.ver,t1.uID,t1.tcKeyPoint,t1.tcProcess,t1.tcResExp,t1.tcTestData,t1.numKeyPoint,t1.tcName FROM tms_testcase t1, tms_mrtcmapping t2 WHERE t1.uID = t2.tcuid AND t2.tpid=? AND t2.mrid=?";
		String returnResult = "";
		int tpid = Integer.parseInt(request.getParameter("tpid"));
		String mrid = request.getParameter("mrid");
		ArrayList<TestCase> tcs = new ArrayList<TestCase>();
		int ver = 0;
		String submittedBy = request.getParameter("submittedBy");
		String comment = "NoC";
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			ps.setString(2, mrid);
			rs = ps.executeQuery();
			
			while (rs.next()){
				TestCase tc = new TestCase();
				tc.setuID(rs.getInt("uID"));
				tc.setTcName(rs.getString("tcName"));
				tc.setTcKeyPoint(rs.getString("tcKeyPoint"));
				tc.setTcProcess(rs.getString("tcProcess"));
				tc.setTcResExp(rs.getString("tcResExp"));
				tc.setTcTestData(rs.getString("tcTestData"));
				tc.setNumKeyPoint(rs.getInt("numKeyPoint"));
				ver = rs.getInt("ver");
				tcs.add(tc);
			}
			/** 取得測試人員送審案例時的註解 **/
			if (!"tester".equalsIgnoreCase(submittedBy)){
				strSQL = "SELECT comment FROM tms_mrcomment WHERE tpid = ? AND mrid = ? AND ver = ? AND creatorGroup = ? ORDER BY sn DESC";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, tpid);
				ps.setString(2, mrid);
				ps.setInt(3, ver);
				ps.setString(4, "tester");
				rs = ps.executeQuery();
				
				if (rs.next()) {
					comment = rs.getString("comment");
				}
			}
			returnResult = new Gson().toJson(tcs);
			returnResult = returnResult.substring(1, returnResult.length());
			if ("tester".equalsIgnoreCase(submittedBy)){
				returnResult = "{\"sqlData\":["+returnResult+",\"strSQL\":\""+strSQL+"****"+tpid+"\",\"ver\":\""+ver+"\",\"size\":\""+tcs.size()+"\"}";	
			} else {
				returnResult = "{\"sqlData\":["+returnResult+",\"strSQL\":\""+strSQL+"****"+tpid+"\",\"comment\":\""+comment+"\",\"size\":\""+tcs.size()+"\"}";
			}
		} catch (SQLException e) {
			returnResult = "{\"errorMsg\":\""+wid.keepErrorMes(conn, ps, e,"", true)+"\"}";
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString();
			}
		}
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}

}
